package com.sina.interfaces.manage.web.controller.sys;


import com.sina.interfaces.core.exception.server.UpdateException;
import com.sina.interfaces.core.web.BaseController;
import com.sina.interfaces.domain.sys.Role;
import com.sina.interfaces.manage.service.sys.RoleManageService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-03-16:44
 */
@Controller
@RequestMapping("roleManage")
public class RoleManageController extends BaseController {

    @Autowired
    @Qualifier("roleManageService")
    private RoleManageService roleManageService;

    @RequiresRoles("admin")
    @RequestMapping(value = "getRoleList", method = RequestMethod.GET)
    public ModelAndView getRoleList(@Valid Role role) {
        LOG.info("role:{}", role);

        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("resultView");
        Long count[] = new Long[1];
        List<Map<String, Object>> roleList = roleManageService.getRoleList(role, count);
        result.put("data", roleList);
        result.put("recordsFiltered", count[0]);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequiresRoles("admin")
    @RequestMapping(value = "getRole", method = RequestMethod.GET)
    public ModelAndView getRole(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        Role role = roleManageService.getRoleByName(name);
        mv.addObject(RESULT, role);

        return mv;
    }


    //支持批量删除，各个id之间用“，”隔开
    @RequiresRoles("admin")
    @RequestMapping(value = "deleteRole", method = RequestMethod.GET)
    public ModelAndView deleteRole(String id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            roleManageService.deleteRole(Integer.valueOf(id));
            result.put(CODE, OK);
            result.put(MSG, "删除角色成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }
        mv.addObject(RESULT, result);
        return mv;
    }

    @RequiresRoles("admin")
    @RequestMapping(value = "updateRole", method = RequestMethod.POST)
    public ModelAndView updateRole(@Valid Role role) {
        LOG.info("role:{}", role);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            roleManageService.updateRole(role);
            result.put(CODE, OK);
            result.put(MSG, "更新角色成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequiresRoles("admin")
    @RequestMapping(value = "saveRole", method = RequestMethod.POST)
    public ModelAndView saveRole(@Valid Role role) {
        LOG.info("role:{}", role);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            roleManageService.saveRole(role);
            result.put(CODE, OK);
            result.put(MSG, "新增角色成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getRoleListToHtml", method = RequestMethod.GET)
    public ModelAndView getRoleListToHtml() {

        ModelAndView mv = new ModelAndView("sys/roleList");
        return mv;
    }

}
